package com.wyg.ep_mail.common.uti;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

//import lombok.extern.slf4j.Slf4j;

/**
 * @Author: WangYongGan
 * @Date: 2022/2/18 18:52
 * @Description:
 */
//@Slf4j
@Component
public class EmailUtil implements InitializingBean {
    public static String QQ = "2245316014@qq.com";
    @Autowired
    private JavaMailSender jms_;
    private static JavaMailSender jms;

    @Override
    public void afterPropertiesSet() throws Exception {
        jms = jms_;
    }

    public static void sendEmail(String from, String to) {
        sendEmail(from, to, "阿里云-数据库-疑似被盗", "请检查 114.55.253.244");
    }

    public static void sendEmail(String to) {
        sendEmail(QQ, to, "阿里云-数据库-疑似被盗", "请检查 114.55.253.244");
    }

    public static void sendEmail(String to, String subject, String text) {
        sendEmail(QQ, to, subject, text);
    }

    public static void sendEmail(String from, String to, String subject, String text) {

        try {
//            log.error("发送邮件-from:" + from + "to:" + to);
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to); // 接收地址
            message.setSubject(subject); // 标题
            message.setText(text); // 内容
            jms.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void sendEmail(String from, String[] tos) {
        for (String to : tos) {
            sendEmail(from, to);
        }
    }

}
